<?php
/**
 * Created by PhpStorm.
 * User: 4399
 * Date: 2017/9/11
 * Time: 16:34
 */
namespace app\admin\controller;
use think\Request;
use \think\Session;
use \think\Cookie;
use \user\Auth;
use think\Controller;

class Banner extends Controller
{

    protected $beforeActionList = [
        'first',
    ];

    protected function first()
    {
        Cookie::init(['prefix'=>'admin_','path'=>'/','httponly'=>true]);
        $token = Cookie::get('token');
        $request = Request::instance();
        $check_res = Auth::check($token,true,Session::get('id'),$request->controller(),$request->action());
        if($check_res['resCode'] == -8){
            Session::delete('id');
            Session::delete('username');
            Session::delete('username_dsp');
            Cookie::delete('token');
            format(-999,$check_res['msg']);
        }else if ($check_res['resCode'] != 2){
            format(-2,$check_res['msg']);
        }
    }

    public function bannerDetail(){
        $request = Request::instance();
        $id = $request->post('id');
        $id = intval($id);
        if ($id > 0) {
            $partnerModel = model('Banner');
            $result = $partnerModel->where('id',$id)->find();
            if ($result) {
                format(0, 'success', $result);
            } else {
                format(-2, 'fail');
            }
        } else {
            format(-2, 'fail');
        }
    }

    public function bannerSave(){
        $request = Request::instance();
        $data['img'] = $request->post('img','', 'SAFE_REPLACE,REMOVE_XSS');
        $data['url'] = $request->post('url','', 'SAFE_REPLACE,REMOVE_XSS');
        $data['title'] = $request->post('title','', 'SAFE_REPLACE,REMOVE_XSS');
        $data['order'] = $request->post('order','', 'SAFE_REPLACE,REMOVE_XSS');
        if ($request->has('status')) {
            $data['status'] = $request->post('status');
        }

        if (!empty($data['status']) && $data['status'] == true) {
            $data['status'] = 1;
        } else {
            $data['status'] = 2;
        }

        if(empty($data['img'])){
            format(-2, "请上传轮播图片");
        }

        if(empty($data['order']) || !is_numeric($data['order'])){
            $data['order'] = 0;
        }

        $bannerModel = model('Banner');
        $data['create_time'] = time();
        $res = $bannerModel->data($data)->save();

        if($res){
            format(0, "添加成功!");
        }else{
            format(-2, "保存失败，请稍后再试!");
        }

    }

    public function bannerEdit(){
        $request = Request::instance();
        $id = $request->post('id',0);
        $data['img'] = $request->post('img','', 'SAFE_REPLACE,REMOVE_XSS');
        $data['url'] = $request->post('url','', 'SAFE_REPLACE,REMOVE_XSS');
        $data['title'] = $request->post('title','', 'SAFE_REPLACE,REMOVE_XSS');
        $data['order'] = $request->post('order','', 'SAFE_REPLACE,REMOVE_XSS');
        if ($request->has('status')) {
            $data['status'] = $request->post('status');
        }

        if (!empty($data['status']) && $data['status'] == true) {
            $data['status'] = 1;
        } else {
            $data['status'] = 2;
        }
        $id = intval($id);

        if($id<=0){
            format(-2, "该轮播不存在");
        }

        if(empty($data['img'])){
            format(-2, "请上传轮播图片");
        }

        if(empty($data['order']) || !is_numeric($data['order'])){
            $data['order'] = 0;
        }

        $bannerModel = model('Banner');
        $result = $bannerModel->where('id',$id)->find();
        if($result) {
            $res = $bannerModel->save($data,['id' => $result['id']]);
        }else{
            format(-2, "该轮播不存在");
        }

        if($res){
            format(0, "保存成功!");
        }else{
            format(-2, "保存失败，请稍后再试!");
        }

    }

    public function bannerList()
    {
        $request = Request::instance();
        $page = $request->post('page');
        $pageSize = $request->post('ps');
        $sort = $request->post('sort', '', 'SAFE_REPLACE,REMOVE_XSS');

        $page = intval($page);
        $pageSize = intval($pageSize);

        if (empty($page)) {
            $page = 1;
        }

        if (empty($pageSize)) {
            $pageSize = 15;
        }

        switch ($sort) {
            case 'id_desc' :
                $order = array('id', 'desc');
                break;
            case 'id_asc' :
                $order = array('id', 'asc');
                break;
            case 'status_desc' :
                $order = array('status', 'desc');
                break;
            case 'status_asc' :
                $order = array('status', 'asc');
                break;
            case 'ct_asc' :
                $order = array('create_time', 'asc');
                break;
            default :
                $order = array('create_time', 'desc');
                break;
        }

        $BannerModel = model('Banner');

        $ret['bannerList'] = $BannerModel->getList( $order, $page, $pageSize);
        $ret['amount'] = $BannerModel->listCount();
        $ret['page'] = $page;
        $ret['pageSize'] = $pageSize;

        format(0, 'success', $ret);
    }

    public function bannerDel()
    {
        $request = Request::instance();
        $id = $request->post('id');
        $id = intval($id);
        if ($id > 0) {
            $partnerModel = model('Banner');
            $result = $partnerModel->where('id',$id)->delete();
            if ($result) {
                format(0, '删除成功！');
            } else {
                format(-2, '删除失败，请稍后再试！');
            }
        } else {
            format(-2, '该轮播信息不存在！');
        }
    }

}